/**
 * @author fanxuean
 * @link fanshine124@gmail.com
 * @description 任务评价列表页面包装器，采用惰性实例化单体模式，入口是init函数，返回包装器对象
 * @return Object {}
 */
MIDB.Model.CommentList = (function(){
  var uniqueInstance;
  function constructor() {
    var p = {};
    var curPage = 1;
    var pages = 1;
    var pageSize = 30;
    var getCommentList = function() {
      var param = {
        userType: MIDB.cache.user.user_type,
        userCode: MIDB.cache.user.user_code,
        userName: MIDB.cache.user.user_name,
        page: curPage,
        pageSize: pageSize
      };
      $.ajax({
        url: MIDB.ajaxUrl.comment_list,
        type: 'POST',
        dataType: 'json',
        data: param,
        complete: function(xhr, textStatus) {
          //called when complete
        },
        success: function(data, textStatus, xhr) {
          if(!!data && data.Rows) {
            compileCommentList(data.Rows);
            compilePagination(data.curPage, data.pages);
          }
        },
        error: function(xhr, textStatus, errorThrown) {
          //called when there is an error
        }
      });
    };
    var compileCommentList = function(data) {
      var tmpl = p.tmpl.html(),
        doTtmpl = doT.template(tmpl),
        fragment = document.createDocumentFragment();
      $(data).each(function(event) {
        var divContainer = document.createElement('div');
        divContainer.innerHTML = doTtmpl(this);
        fragment.appendChild(divContainer);
      });
      p.tmplContainer.html(fragment);
    };
    /**
     * [compilePagination 渲染分页条]
     * @param  {[Integer]} cur [当前页]
     * @param  {[Integer]} all [总页数]
     * @return {[Null]}        [无返回]
     */
    var compilePagination = function(cur, all) {
        var pageContainer = $(p.contentContainer).find('.pagination'),
            tmpl = p.pageTmpl.html(),
            doTtmpl = doT.template(tmpl);
        curPage = cur;
        pages = all;
        pageContainer.html(doTtmpl({curPage: cur, pages: pages}));
        bindPageEvent(pageContainer);
    };
    /**
     * [bindPageEvent 绑定翻页事件]
     * @param  {[Object]} pageContainer [分页条对象]
     * @return {[Null]}               [无返回]
     */
    var bindPageEvent = function(pageContainer) {
        $(pageContainer)
        .find('.prePage').click(function(event) {
            if(curPage > 1) curPage--;
            getCommentList();
            event.stopPropagation();
        }).end()
        .find('.nextPage').click(function(event) {
            if(curPage < pages) curPage++;
            getCommentList();
            event.stopPropagation();
        }).end()
        .find('select[name=jumpPage]').change(function(event) {
            curPage = $(this).val();
            getCommentList();
            event.stopPropagation();
        });
    };
    return {
      init: function(options) {
        p = $.extend({}, p||{}, options || {});
        getCommentList();
        // compileCommentList(missionComment);
        return this;
      }
    };
  }
  return {
    getInstance : function() {
      if (!uniqueInstance) {
          uniqueInstance = constructor();
      }
      return uniqueInstance;
    }
  };
})();